package me.monster.smarttraffic.ui.car;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.monster.smarttraffic.base.BasePresenter;
import me.monster.smarttraffic.net.RemoteService;
import me.monster.smarttraffic.tool.StringUtil;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-12 10:33
 */
public class CarPresenter extends BasePresenter<CarContract.View> implements CarContract.Presenter {
    public static final int ERROR_NO_SELECT = 100;
    public static final int ERROR_NO_INPUT = 200;
    public static final int ERROR_CAR_LIST = 300;
    private static final String TAG = "CarPresenter";
    private List<String> carNoList = new ArrayList<>();

    @Override
    public void listCar() {
        iView.showLoading();
        Disposable listCarDis = RemoteService.getInstance()
                .listCar()
                .doOnNext(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {
                        carNoList.clear();
                        carNoList.addAll(strings);
                    }
                })
                .map(new Function<List<String>, List<Map<String, String>>>() {
                    @Override
                    public List<Map<String, String>> apply(List<String> strings) throws Exception {
                        List<Map<String, String>> mapList = new ArrayList<>(strings.size());
                        for (String string : strings) {
                            Map<String, String> map = new HashMap<>();
                            map.put("no", string);
                            mapList.add(map);
                        }
                        return mapList;
                    }
                }).subscribe(new Consumer<List<Map<String, String>>>() {
                    @Override
                    public void accept(List<Map<String, String>> maps) throws Exception {
                        iView.dismissLoading();
                        iView.bindCars(maps);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        handleError(throwable);
                        iView.dismissLoading();
                        iView.showError(ERROR_CAR_LIST);
                    }
                });
        addDispose(listCarDis);
    }

    @Override
    public void query(int carInfoPosition) {
        if (carInfoPosition < 0) {
            iView.showError(ERROR_NO_SELECT);
            return;
        }
        iView.showLoading();
        String no = carNoList.get(carInfoPosition);
        Disposable queryDis = RemoteService.getInstance().queryCar(no)
                .subscribe(new Consumer<Map<String, String>>() {
                    @Override
                    public void accept(Map<String, String> stringStringMap) throws Exception {
                        iView.dismissLoading();
                        stringStringMap.put("carNo", no);
                        stringStringMap.put("now", String.valueOf(new Random().nextInt(500)));
                        iView.showCarInfo(stringStringMap);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.dismissLoading();
                        handleError(throwable);
                        iView.toast("查询失败");
                    }
                });
        addDispose(queryDis);
    }

    @Override
    public void charge(int carInfoPosition, String input) {
        if (carInfoPosition < 0) {
            iView.showError(ERROR_NO_SELECT);
            return;
        }
        if (StringUtil.isEmpty(input)) {
            iView.showError(ERROR_NO_INPUT);
            return;
        }
        iView.showLoading();
        Disposable chargeDis = RemoteService.getInstance().chargeCar(carNoList.get(carInfoPosition), input)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        iView.dismissLoading();
                        iView.chargeResult(true);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        iView.dismissLoading();
                        iView.chargeResult(false);
                    }
                });
        addDispose(chargeDis);
    }
}
